const ErrorProxy = require('../exception/BusinessExceptionProxy');

/**
 * Auth Util
 */
class AuthUtil {
  /**
   * get username from event
   * @param {Object<event>} event
   * @param {String} event.requestContext.authorizer.claims['cognito:username']
   * @return {String}
   */
  static getUsername(event) {
    let username = null;

    try {
      console.info('auth user:', JSON.stringify(event.requestContext.authorizer.claims));
      username = event.requestContext.authorizer.claims['cognito:username'];
    } catch (e) {}

    if (!username) {
      ErrorProxy.unknownAuthorizer();
    }

    return username;
  }
  /**
   * get username from event
   * @param {Object<event>} event
   * @return {String}
   */
  static getUserInfoFromAuth(event) {
    let user = {};

    try {
      console.info('auth user:', JSON.stringify(event.requestContext.authorizer.claims));
      let claims = event.requestContext.authorizer.claims;
      user.username = claims['cognito:username'];
      user.email = claims['email'];
      user.phone = claims['phone_number'];
      // user.phone_number_verified = claims['phone_number_verified'];
    } catch (e) {
      console.error(e);
    }
    if (!user.username) {
      ErrorProxy.unknownAuthorizer();
    }
    // if (!user.phone_number_verified) {
    //   ErrorProxy.unVerifiedPhoneNumber();
    // }

    return user;
  }
}

module.exports = AuthUtil;
